home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / checkbox / lib / config.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  4.5 KB  |  145 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import os
  20. import re
  21. import logging
  22. import posixpath
  23.  
  24. from ConfigParser import ConfigParser
  25.  
  26.  
  27. class IncludeDict(dict):
  28.  
  29.     def __init__(self, parser):
  30.         super(IncludeDict, self).__init__()
  31.         self._parser = parser
  32.  
  33.         for (key, value) in os.environ.items():
  34.             super(IncludeDict, self).__setitem__(key.lower(), value)
  35.  
  36.     def __setitem__(self, key, value):
  37.         if key == "includes":
  38.             for path in re.split(r"\s*,?\s+", value):
  39.                 path = self._parser._interpolate("DEFAULT", None, path, self)
  40.                 path = posixpath.expanduser(path)
  41.                 if not posixpath.exists(path):
  42.                     raise Exception, "No such configuration file: %s" % path
  43.                 if posixpath.isdir(path):
  44.                     logging.info("Parsing config filenames from directory: %s",
  45.                         path)
  46.                     def walk_func(arg, directory, names):
  47.                         for name in names:
  48.                             path = posixpath.join(directory, name)
  49.                             if not posixpath.isdir(path):
  50.                                 arg._parser.read(path)
  51.  
  52.                     posixpath.walk(path, walk_func, self)
  53.                 else:
  54.                     logging.info("Parsing config filename: %s", path)
  55.                     self._parser.read(path)
  56.  
  57.         # Environment has precedence over configuration
  58.         elif key.upper() not in os.environ.keys():
  59.             super(IncludeDict, self).__setitem__(key, value)
  60.  
  61.  
  62. class ConfigSection(object):
  63.  
  64.     def __init__(self, parent, name, attributes={}):
  65.         self.parent = parent
  66.         self.name = name
  67.         self.attributes = attributes
  68.  
  69.     def __getattr__(self, name):
  70.         if name in self.attributes:
  71.             return self.get(name)
  72.  
  73.         raise AttributeError, name
  74.  
  75.     def __contains__(self, name):
  76.         return name in self.attributes
  77.  
  78.     def get(self, name):
  79.         return self.attributes.get(name, "")
  80.  
  81.     def set(self, name, value):
  82.         self.parent._parser.set(self.name, name, value)
  83.         self.attributes[name] = value
  84.  
  85.  
  86. class ConfigDefaults(ConfigSection):
  87.  
  88.     def __getattr__(self, name):
  89.         if name in self.attributes:
  90.             return self.get(name)
  91.  
  92.         raise AttributeError, name
  93.  
  94.     def get(self, name):
  95.         return os.environ.get(name.upper()) \
  96.             or os.environ.get(name.lower()) \
  97.             or super(ConfigDefaults, self).get(name)
  98.  
  99.  
  100. class Config(object):
  101.  
  102.     def __init__(self, path, configs=[]):
  103.         self.path = path
  104.  
  105.         self._parser = ConfigParser()
  106.         self._parser._defaults = IncludeDict(self._parser)
  107.  
  108.         if not posixpath.exists(path):
  109.             raise Exception, "No such configuration file: %s" % path
  110.         self._parser.read(path)
  111.  
  112.         for config in configs:
  113.             match = re.match("(.*)/([^/]+)=(.*)", config)
  114.             if not match:
  115.                 raise Exception, "Invalid config string: %s" % config
  116.  
  117.             (name, option, value) = match.groups()
  118.             if not self._parser.has_section(name):
  119.                 self._parser.add_section(name)
  120.  
  121.             self._parser.set(name, option, value)
  122.  
  123.         # Copy attributes from the parser to avoid one additional
  124.         # function call on each access.
  125.         for attr in ["has_section", "remove_section"]:
  126.             setattr(self, attr, getattr(self._parser, attr))
  127.  
  128.     def get_defaults(self):
  129.         attributes = self._parser.defaults()
  130.         return ConfigDefaults(self, 'DEFAULT', attributes)
  131.  
  132.     def get_section(self, name):
  133.         if self._parser.has_section(name):
  134.             attributes = dict(self._parser.items(name))
  135.             return ConfigSection(self, name, attributes)
  136.  
  137.         return None
  138.  
  139.     def get_section_names(self):
  140.         return self._parser.sections()
  141.  
  142.     def add_section(self, name):
  143.         self._parser.add_section(name)
  144.         return self.get_section(name)
  145.